import java.util.Arrays;

public class MatchNumbersEasy {
	public String[] dp;

	public String maxNumber(int[] matches, int n) {
		int N = matches.length;
		dp = new String[55];
		for (int i = 1; i <= n; i++)
			dp[i] = "";
		for (int i = 0; i < N; i++) {
			dp[matches[i]] = Integer.toString(i);
		}

		for (int i = 2; i <= n; i++) {
			for (int j = 1; j <= i / 2; j++) {
				String tmp = dp[j] + dp[i - j];
				tmp = sortReversely(tmp);
				if (tmp.length() > dp[i].length() && tmp.charAt(0) != '0')
					dp[i] = tmp;
				else if (tmp.length() == dp[i].length()
						&& tmp.compareTo(dp[i]) > 0)
					dp[i] = tmp;
			}
		}
		if (dp[n].length() == 0 || dp[n].charAt(0) == '0')
			return "0";
		return dp[n];
	}

	public static String sortReversely(String s) {
		char[] temp = s.toCharArray();
		Arrays.sort(temp);
		String ret = new String(temp);
		ret = new StringBuffer(ret).reverse().toString();
		return ret;
	}

	 public static void main(String[] args)
	{
		long time;
		String answer;
		boolean errors = false;
		String desiredAnswer;
		
		
		time = System.currentTimeMillis();
		answer = new MatchNumbersEasy().maxNumber(new int[]{6,7,8}, 21);
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = "210";
		System.out.println("Your answer:");
		System.out.println("\t\"" + answer + "\"");
		System.out.println("Desired answer:");
		System.out.println("\t\"" + desiredAnswer + "\"");
		if (!answer.equals(desiredAnswer))
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new MatchNumbersEasy().maxNumber(new int[]{5,23,24}, 30);
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = "20";
		System.out.println("Your answer:");
		System.out.println("\t\"" + answer + "\"");
		System.out.println("Desired answer:");
		System.out.println("\t\"" + desiredAnswer + "\"");
		if (!answer.equals(desiredAnswer))
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new MatchNumbersEasy().maxNumber(new int[]{1,5,3,2}, 1);
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = "0";
		System.out.println("Your answer:");
		System.out.println("\t\"" + answer + "\"");
		System.out.println("Desired answer:");
		System.out.println("\t\"" + desiredAnswer + "\"");
		if (!answer.equals(desiredAnswer))
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new MatchNumbersEasy().maxNumber(new int[]{1,1,1,1,1,1,1,1,1,1}, 50);
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = "99999999999999999999999999999999999999999999999999";
		System.out.println("Your answer:");
		System.out.println("\t\"" + answer + "\"");
		System.out.println("Desired answer:");
		System.out.println("\t\"" + desiredAnswer + "\"");
		if (!answer.equals(desiredAnswer))
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		
		
		if (errors)
			System.out.println("Some of the test cases had errors :-(");
		else
			System.out.println("You're a stud (at least on the test data)! :-D ");
	}

}
// Powered by [KawigiEdit] 2.0!
